﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Utility.XML
{
    public class Xml
    {
        #region 成员变量
        private string _FileName;
        private XmlDocument _XmlDocument;
        #endregion

        #region 构造函数
        public Xml()
        {
            this._FileName = "";
            this._XmlDocument = new XmlDocument();
        }

        public Xml(string fileName)
        {
            this._FileName = fileName;
            this._XmlDocument = new XmlDocument();
            try
            {
                this._XmlDocument.Load(this._FileName);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region 属性
        #region FileName
        public string FileName
        {
            get
            {
                return this._FileName;
            }

            set
            {
                this._FileName = value;
            }
        }
        #endregion

        #region XmlDocument
        public XmlDocument XmlDocument
        {
            get
            {
                return this._XmlDocument;
            }

            set
            {
                this._XmlDocument = value;
            }
        }
        #endregion
        #endregion

        #region 方法
        #region Reload
        /// <summary>
        /// When the Xml file name change, Reload the new one
        /// </summary>
        public void Reload()
        {
            this._XmlDocument.Load(this._FileName);
        }
        #endregion

        #region GetElementValue
        /// <summary>
        /// GetNodeValue
        /// </summary>
        /// <param name="nodeName">Node name in a XML document</param>
        /// <param name="addKey">The key of the value node you want to find</param>
        /// <returns>Return the value of the key and return "" if not found</returns>
        public string GetElementValue(string nodeName, string addKey)
        {
            try
            {
                XmlNode xmlNode = this._XmlDocument.DocumentElement;
                string xPath = "descendant::" + nodeName;
                xmlNode = xmlNode.SelectSingleNode(xPath);
                if (xmlNode == null)
                {
                    throw new Exception("Xmlnode not found");
                }
                xPath = "descendant::add[@key=\'" + addKey + "\']";
                xmlNode = xmlNode.SelectSingleNode(xPath);
                if (xmlNode != null)
                {
                    return xmlNode.Attributes["value"].Value;
                }
                else
                {
                    throw new Exception("Xmlnode not found");
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region SetElementValue
        /// <summary>
        /// SetElementValue
        /// </summary>
        /// <param name="nodeName">Node name in a XML document</param>
        /// <param name="addKey">The key of the value node you want to replace</param>
        /// <returns>The value you want to replace the key with</returns>
        public void SetElementValue(string nodeName, string addKey, string value)
        {
            try
            {
                XmlNode xmlNode = this._XmlDocument.DocumentElement;
                string xPath = "descendant::" + nodeName;
                xmlNode = xmlNode.SelectSingleNode(xPath);
                if (xmlNode == null)
                {
                    throw new Exception("Xmlnode not found");
                }

                xPath = "descendant::add[@key=\'" + addKey + "\']";
                xmlNode = xmlNode.SelectSingleNode(xPath);
                if (xmlNode != null)
                {
                    xmlNode.Attributes["value"].Value = value;
                    this._XmlDocument.Save(this._FileName);
                }
                else
                {
                    throw new Exception("Xmlnode not found");
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region AddElement
        /// <summary>
        /// AddElement
        /// </summary>        
        /// <param name="nodeName">Node name in a XML document</param>
        /// <param name="addKey">The name of the key you want to add</param>
        /// <param name="value">The value of the key you want to add</param>
        public void AddElement(string nodeName, string addKey, string value)
        {
            string xPath = "descendant::" + nodeName;
            XmlNode xmlNode = this._XmlDocument.SelectSingleNode(xPath);

            XmlElement xmlElement = this._XmlDocument.CreateElement("add");
            xmlElement.SetAttribute("key", addKey);
            xmlElement.SetAttribute("value", value);
            xmlNode.AppendChild(xmlElement);

            try
            {
                this._XmlDocument.Save(this._FileName);

            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region LoadAppConfig
        public static string LoadAppConfig(string keyName)
        {
            try
            {
                return System.Configuration.ConfigurationSettings.AppSettings[keyName].ToString();
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #endregion
    }
}